#include "battle.h"
#include "ui_battle.h"
#include <QObject>

battle::battle(QWidget *parent)
    : QWidget(parent),
     ui(new Ui::battle),
    m_playerPokemon(nullptr),
    m_enemyPokemon(nullptr),
    m_isPlayerTurn(true)
{
    ui->setupUi(this);
    ui->battleLog->setReadOnly(true);
    ui->skillButton1->setEnabled(false);
    ui->skillButton2->setEnabled(false);
    ui->skillButton3->setEnabled(false);
    ui->skillButton4->setEnabled(false);
    connect(ui->runButton,&QPushButton::clicked,[=](){
        //因为此类里面没有widget的对象，所以想要返回只能用父类的函数调控父类
        emit this->back();
    });
}
battle::~battle()
{
    delete ui;
}



void battle::on_runButton_clicked()
{

}

void battle::setPlayerPokemon(Pokemon* pokemon) {
    m_playerPokemon = pokemon;
    ui->playerImage->setPixmap(pokemon->getImage().scaled(200, 200, Qt::KeepAspectRatio));
    ui->playerName->setText(pokemon->getName());
    ui->playerHP->setText(QString("HP: %1/%2").arg(pokemon->getHP()).arg(pokemon->getMaxHP()));

    connect(pokemon, &Pokemon::hpChanged, [this](int hp, int maxHP) {
        ui->playerHP->setText(QString("HP: %1/%2").arg(hp).arg(maxHP));
    });
 connect(pokemon, &Pokemon::useSkill, this, &battle::onSkillUsed);
    connect(pokemon, &Pokemon::fainted, this, &battle::onFainted);

    const auto& skills = pokemon->getSkills();
    if (skills.size() > 0) ui->skillButton1->setText(skills[0]->getName());
    if (skills.size() > 1) ui->skillButton2->setText(skills[1]->getName());
    if (skills.size() > 2) ui->skillButton3->setText(skills[2]->getName());
    if (skills.size() > 3) ui->skillButton4->setText(skills[3]->getName());

    // 启用技能按钮
    for (int i = 0; i < qMin(4, skills.size()); ++i) {
        QPushButton* btn = this->findChild<QPushButton*>(QString("skillButton%1").arg(i+1));
        if (btn) btn->setEnabled(true);
    }
}

void battle::setEnemyPokemon(Pokemon* pokemon) {
    m_enemyPokemon = pokemon;
    ui->enemyImage->setPixmap(pokemon->getImage().scaled(200, 200, Qt::KeepAspectRatio));
    ui->enemyName->setText(pokemon->getName());
    ui->enemyHP->setText(QString("HP: %1/%2").arg(pokemon->getHP()).arg(pokemon->getMaxHP()));

    connect(pokemon, &Pokemon::hpChanged, [this](int hp, int maxHP) {
        ui->enemyHP->setText(QString("HP: %1/%2").arg(hp).arg(maxHP));
    });
    connect(pokemon, &Pokemon::useSkill, this, &battle::onSkillUsed);
    connect(pokemon, &Pokemon::fainted, this, &battle::onFainted);
}

void battle::startBattle() {
    ui->battleLog->append("战斗开始！");
    m_isPlayerTurn = true;
    updateUI();
}

void battle::on_skillButton1_clicked() {
    if (m_isPlayerTurn && m_playerPokemon) {
        m_playerPokemon->useSkill(0, m_enemyPokemon);
        m_isPlayerTurn = false;
        QTimer::singleShot(1500, this, &battle::enemyTurn);
    }
}

void battle::on_skillButton2_clicked() {
    if (m_isPlayerTurn && m_playerPokemon) {
        m_playerPokemon->useSkill(1, m_enemyPokemon);
        m_isPlayerTurn = false;
        QTimer::singleShot(1500, this, &battle::enemyTurn);
    }
}

void battle::on_skillButton3_clicked() {
    if (m_isPlayerTurn && m_playerPokemon) {
        m_playerPokemon->useSkill(2, m_enemyPokemon);
        m_isPlayerTurn = false;
        QTimer::singleShot(1500, this, &battle::enemyTurn);
    }
}

void battle::on_skillButton4_clicked() {
    if (m_isPlayerTurn && m_playerPokemon) {
        m_playerPokemon->useSkill(3, m_enemyPokemon);
        m_isPlayerTurn = false;
        QTimer::singleShot(1500, this, &battle::enemyTurn);
    }
}

void battle::enemyTurn() {
    if (!m_isPlayerTurn && m_enemyPokemon->isAlive()) {
        ui->battleLog->append(QString("%1的回合！").arg(m_enemyPokemon->getName()));
        int index = rand() % m_enemyPokemon->getSkills().size();
        m_enemyPokemon->useSkill(index, m_playerPokemon);
        m_isPlayerTurn = true;
        checkBattleEnd();
    }
}

void battle::onSkillUsed(int index, Pokemon* user) {
    // 1. 安全检查
    if (!user || index < 0 || index >= user->getSkills().size()) {
        qWarning() << "Invalid skill index or null Pokemon!";
        return;
    }

    // 2. 获取技能和使用者名称
    Skill* skill = user->getSkills()[index];
    QString userName = (user == m_playerPokemon) ?
                           m_playerPokemon->getName() :
                           m_enemyPokemon->getName();

    // 3. 更新战斗日志
    if (ui && ui->battleLog) {
        ui->battleLog->append(QString("%1 使用了 %2！")
                                  .arg(userName)
                                  .arg(skill->getName()));
    }
}


void battle::onFainted() {
    Pokemon* fainted = qobject_cast<Pokemon*>(sender());
    ui->battleLog->append(QString("%1失去战斗能力！").arg(fainted->getName()));
    checkBattleEnd();
}

void battle::checkBattleEnd() {
    if (!m_playerPokemon->isAlive()) {
        ui->battleLog->append("战斗结束！你输了。");
        // 禁用所有技能按钮
        ui->skillButton1->setEnabled(false);
        ui->skillButton2->setEnabled(false);
        ui->skillButton3->setEnabled(false);
        ui->skillButton4->setEnabled(false);
    } else if (!m_enemyPokemon->isAlive()) {
        ui->battleLog->append("战斗结束！你赢了。");
        ui->skillButton1->setEnabled(false);
        ui->skillButton2->setEnabled(false);
        ui->skillButton3->setEnabled(false);
        ui->skillButton4->setEnabled(false);
    }
}


void battle::updateUI() {

    ui->playerHP->setText(QString("HP: %1/%2").arg(m_playerPokemon->getHP()).arg(m_playerPokemon->getMaxHP()));
    ui->enemyHP->setText(QString("HP: %1/%2").arg(m_enemyPokemon->getHP()).arg(m_enemyPokemon->getMaxHP()));

    // 显示当前回合
    ui->battleLog->append(m_isPlayerTurn ? "你的回合！" : QString("%1的回合！").arg(m_enemyPokemon->getName()));

}
